package com.web;

import com.MybatisUtil.MybatisUtil;
import com.MybatisUtil.StringUtil;
import com.bean.Users;
import com.dao.UsersMapper;
import com.service.UsersService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

// http://Localhost:8080/user?op=reg 注册
// http://Localhost:8080/user?op=Login 登录
//路径写法urlPattern="/servlet/HelloServlet" 这是<Form action="servlet /HelloServlet ">
@WebServlet(urlPatterns = "/user", name = "UsersServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String op = req.getParameter("op");
        if("login".equals(op)){
            login(req,resp);
        }else if("reg".equals(op)){
            reg(req,resp);
        }else if("toPassword".equals(op)){
            toPassword(req,resp);
        }else if("doPassword".equals(op)){
            doPassword(req,resp);
        }
        else{
            throw new ServletException("不支持的操作！");
        }
    }
    protected void doPassword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String pwd = StringUtil.requiredString("pwd","原有密码",req);
        //比较原有密码与数据库储存的密码是否一致？
        Users user = (Users) req.getSession().getAttribute("user");
        if (user.getPwd().equals(pwd)){
            if (StringUtil.fieldsCompare("newpwd","repwd",req)){
                String newpwd = StringUtil.requiredString("pwd","新的密码",req);
                UsersService usersService = new UsersService();
                UsersService.updatepwd(user.getId(),newpwd,user);

                StringUtil.toSuccess("操作成功","/succ.jsp","/index.jsp",10,req,resp);
            }

        }else {
            req.setAttribute("pwd_error","原有密码不正确!");

        }




        //String repwd = StringUtil.requiredString("pwd","确认密码",req);


    }



    protected void toPassword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        if (req.getSession().getAttribute("user")!=null){
            req.getRequestDispatcher("/WEB-INF/password.jsp").forward(req,resp);
        }else {
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }

    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String nickname = req.getParameter("account");
        String pwd = req.getParameter("pwd");
        UsersService usersService = new UsersService();
        Users user = usersService.login(nickname,pwd);
        if(user!=null && user.getPwd().equals(pwd)){// 验证密码
               req.getSession().setAttribute("user",user);
               resp.sendRedirect("/index.jsp");
               System.out.println("TEST1");
        }
        else{
              System.out.println("TEST2");
              resp.sendRedirect("/login.jsp");
        }
    }
    private void reg(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String nickname = StringUtil.requiredString("nickname","用户名",req);
        String realname = StringUtil.requiredString("realname","真实姓名",req);
        String pwd = StringUtil.requiredString("pwd","密码",req);
        String repwd = StringUtil.requiredString("repwd","确认密码",req);
        String phone = StringUtil.requiredString("phone","电话",req);
        String email = StringUtil.requiredString("email","邮件",req);
        String address = req.getParameter("address");
        String realid = req.getParameter("realid");
        if(nickname == null ||
                realname == null ||
                pwd == null ||
                repwd == null ||
                phone == null ||
                email == null ||
                !StringUtil.fieldsCompare("pwd","repwd",req)
        ){
                req.getRequestDispatcher("/reg.jsp").forward(req,resp);
                 return;
        }
        // 构建业务对象
        Users user = new Users(nickname,realname,pwd,phone,email,address,new Date(),1,realid);
        UsersService usersService = new UsersService();
        usersService.reg(user);
        resp.sendRedirect("/login.jsp");
    }


}
